﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Engine.Cameras;

namespace Engine.SceneControl.Scene
{
    public class Render : IRenderHelper
    {
        public Render(EngineStuff engine)
        {
            this._engine = engine;
        }

        #region IRender Members
        private EngineStuff _engine;        
        private RenderTarget _target;
        private RenderType _type;
        private IWorld _world;
        private IDictionary<String, Texture2D> _dic = new Dictionary<String, Texture2D>();
        private String _currentTechnic;
        private QuadRenderer quad = new QuadRenderer();

        public void SetUpRenderTarget(RenderTarget target, RenderType type)
        {
            this._target = target;
            this._type = type;
        }


        public void RenderSceneWithMaterial(string scene) 
        {
            RenderSceneWithMaterial(scene,null);
        }


        public void RenderSceneWithMaterial(string scene, String cameraName)
        {
            switch (_target)
            {
                case RenderTarget.VIDEO:
                    _engine.PreRender(_engine.GetCurrentGameTime());
                    _world.DrawnWorld(_engine.GetCurrentGameTime(),this);
                    _engine.PosRender(_engine.GetCurrentGameTime());
                    break;
                case RenderTarget.TEXTURE2D:
                    GraphicsDevice gd = _engine.GraphicsDevice;
                    PresentationParameters pp = _engine.GraphicsDevice.PresentationParameters;
                    RenderTarget2D renderTarget = new RenderTarget2D(gd,
                       pp.BackBufferWidth,
                       pp.BackBufferHeight,
                       1,
                       SurfaceFormat.Color);

                    RenderTarget2D oldTarget = _engine.GraphicsDevice.GetRenderTarget(0) as RenderTarget2D;

                    switch (_type)
                    {
                        case RenderType.NORMALSCENE:
                            _engine.GraphicsDevice.SetRenderTarget(0, renderTarget);
                            break;
                        default:
                            throw new Exception("Target nao suportado");
                    }

                    
            
                    if (cameraName != null)
                    {
                        String defaultCamera = _world.ActiveCameraName();
                        _world.setActiveCamera(cameraName);
                        _engine.PreRender(_engine.GetCurrentGameTime());
                        _world.DrawnWorld(_engine.GetCurrentGameTime(),this);
                        _engine.PosRender(_engine.GetCurrentGameTime());
                        _world.setActiveCamera(defaultCamera);
                    }
                    else
                    {
                        _engine.PreRender(_engine.GetCurrentGameTime());
                        _world.DrawnWorld(_engine.GetCurrentGameTime(),this);
                        _engine.PosRender(_engine.GetCurrentGameTime());
                    }                   

                    _engine.GraphicsDevice.SetRenderTarget(0, oldTarget);
                    _dic[scene] = renderTarget.GetTexture();
                  
            
                    break;
            }

            
            
        }

        public Microsoft.Xna.Framework.Graphics.Texture2D this[string scene]
        {
            get { return _dic[scene]; }
            set
            {
                _dic[scene] = value;
            }
        }

        #endregion

        #region IRender Members

        public void setWorld(IWorld world)
        {
            this._world = world;
        }

        #endregion

        #region IRender Members

        public string CurrentTechnic
        {
            get
            {
                return _currentTechnic;
            }
            set
            {
                this._currentTechnic = value;
            }
        }

        #endregion

        #region IRender Members


        

        public void RenderTextureToScreen(Texture2D scene)
        {
            quad.DrawQuad(_engine.GraphicsDevice, scene);
        }

        #endregion

        #region IRender Members


        public void SetQuadEffect(string effectPath)
        {
            quad.LoadContent(_engine, effectPath);
        }

        #endregion

        #region IRender Members


        public void RenderTextureToScreen(string scene)
        {
            quad.DrawQuad(_engine.GraphicsDevice, this[scene]);
        }

        #endregion


        #region IRender Members


        public EffectParameterCollection GetEffectParameters()
        {
            return quad.Parameter;
        }

        #endregion
    }
}

